package com.tengroup.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tengroup.entity.Question;
import com.tengroup.service.QuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/questions")
public class QuestionController {

    @Autowired
    private QuestionService questionService;

    // 增加题目
    @PostMapping
    public boolean addQuestion(@RequestBody Question question) {
        return questionService.addQuestion(question);
    }
    @PostMapping("/import")
    public String importQuestions(@RequestBody List<Question> questions) {
        boolean result = questionService.importQuestions(questions);
        return result ? "导入成功" : "导入失败";
    }
    // 删除题目（软删除）
    @DeleteMapping("/{id}")
    public boolean deleteQuestion(@PathVariable Long id) {
        return questionService.deleteQuestion(id);
    }

    // 修改题目
    @PutMapping
    public boolean updateQuestion(@RequestBody Question question) {
        return questionService.updateQuestion(question);
    }

    // 根据 ID 查询题目
    @GetMapping("/{id}")
    public Question getQuestionById(@PathVariable Long id) {
        return questionService.getQuestionById(id);
    }

    // 查询所有未删除的题目
    @GetMapping
    public List<Question> getAllQuestions() {
        return questionService.getAllQuestions();
    }

    // 分页查询所有未删除的题目
    @GetMapping("/page")
    public IPage<Question> getAllQuestions(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size) {
        return questionService.getAllQuestions(page, size);
    }

    // 根据题目类型查询题目列表
    @GetMapping("/type/{questionType}")
    public List<Question> findByQuestionType(@PathVariable Integer questionType) {
        return questionService.findByQuestionType(questionType);
    }

    // 根据科目 ID 查询题目列表
    @GetMapping("/subject/{subjectId}")
    public List<Question> findBySubjectId(@PathVariable Integer subjectId) {
        return questionService.findBySubjectId(subjectId);
    }

    // 根据难度级别查询题目列表
    @GetMapping("/difficulty/{difficulty}")
    public List<Question> findByDifficulty(@PathVariable Byte difficulty) {
        return questionService.findByDifficulty(difficulty);
    }

    // 根据关键字模糊查询题目列表
    @GetMapping("/search")
    public List<Question> findByKeyword(@RequestParam String keyword) {
        return questionService.findByKeyword(keyword);
    }

    // 查询所有已删除的题目
    @GetMapping("/deleted")
    public List<Question> findAllDeletedQuestions() {
        return questionService.findAllDeletedQuestions();
    }
    //考题去重
    @DeleteMapping("/duplicates")
    public String removeDuplicates() {
        questionService.removeDuplicateQuestions();
        return "重复题目已删除";
    }

}